package com.terry.gamehall.test;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CompletableFutureTest4 {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);
    public static void main(String[] args) {
        //两任务组合 一个完成即可
        //applyToEither :两个任务有一个执行完成,获取它的返回值,处理任务并有新的返回值
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一"+Thread.currentThread().getId()+"开始");
            int i = 10 / 2;
            System.out.println("运行结果 " + i);
            System.out.println("任务一"+Thread.currentThread().getId()+"结束");
            return i+"";
        }, executor);


        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务二"+Thread.currentThread().getId()+"开始");
            System.out.println("任务二"+Thread.currentThread().getId()+"结束");
            return "hello";
        }, executor);
        future.applyToEitherAsync(future2,(r1)->{
            return "";
        }, executor);
        //acceptEither:两个任务有一个执行完成,获取它的返回值,处理任务没有返回值
        //runAfterEither:两个任务有一个执行完成,不需要获取它的返回值,处理任务没有返回值
    }

}
